package org.cuieney.videolife.ui.fragment.short_video;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.transition.Fade;
import android.transition.Slide;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewParent;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import org.cuieney.videolife.App;
import org.cuieney.videolife.R;
import org.cuieney.videolife.common.base.BaseFragment;
import org.cuieney.videolife.common.base.DetailTransition;
import org.cuieney.videolife.common.component.EventUtil;
import org.cuieney.videolife.common.okhttp.http.OkHttpClientManager;
import org.cuieney.videolife.common.utils.DouyinUtils;
import org.cuieney.videolife.common.utils.ToastUtil;
import org.cuieney.videolife.entity.MusicListBean;
import org.cuieney.videolife.entity.ShortVideoBean.DouyinVideoListData;
import org.cuieney.videolife.entity.ShortVideoBean.LevideoData;
import org.cuieney.videolife.presenter.ShortVideoHomePresenter;
import org.cuieney.videolife.presenter.contract.ShortVideoHomeContract;
import org.cuieney.videolife.ui.act.DouyinActivity;
import org.cuieney.videolife.ui.adapter.DouYinAdapter;
import org.cuieney.videolife.ui.adapter.MusicAdapter;
import org.cuieney.videolife.ui.adapter.ShortVideoAdapter;
import org.cuieney.videolife.ui.fragment.music.MusicDetailFragment;
import org.cuieney.videolife.ui.video.JumpUtils;
import org.cuieney.videolife.ui.widget.EndLessOnScrollListener;
import org.cuieney.videolife.ui.widget.VerticalViewPager;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import de.hdodenhof.circleimageview.CircleImageView;
import okhttp3.Request;

/**
 * Created by cuieney on 17/3/4.
 */

public class ShortVideoHomeFragment extends BaseFragment<ShortVideoHomePresenter> implements ShortVideoHomeContract.View  {
    private static final String TAG = "ShortVideoHomeFragment";

    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;

    private List<LevideoData> mShortVideoList = new ArrayList<>();
    private ShortVideoAdapter adapter;
    private long max_cursor = 0;

    public static ShortVideoHomeFragment newInstance() {
        Bundle bundle = new Bundle();
        ShortVideoHomeFragment shortVideoHomeFragment = new ShortVideoHomeFragment();
        shortVideoHomeFragment.setArguments(bundle);
        return shortVideoHomeFragment;
    }
    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_short_video;
    }

    @Override
    protected void initEventAndData() {
        initData();
        /*int ts = (int) (System.currentTimeMillis() / 1000);
        String as_cp = DouyinUtils.getASCP(getActivity(), 0, 0, ts);
        String asStr = as_cp.substring(0, as_cp.length() / 2);
        String cpStr = as_cp.substring(as_cp.length() / 2, as_cp.length());
        Log.e(TAG, "asStr=" + asStr + " cpStr" + cpStr);

        refresh.setProgressViewOffset(false,100,200);
        refresh.setOnRefreshListener(() -> mPresenter.getVideoData(asStr, cpStr, (int) (System.currentTimeMillis() / 1000)));

        GridLayoutManager layout = new GridLayoutManager(getActivity(), 2);
        recycler.setLayoutManager(layout);

        recycler.addOnScrollListener(new EndLessOnScrollListener(layout,1) {
            @Override
            public void onLoadMore() {
                mPresenter.getVideoData(asStr, cpStr, (int) (System.currentTimeMillis() / 1000));
            }
        });
        adapter = new ShortVideoAdapter(getActivity(), mShortVideoList);
        recycler.setAdapter(adapter);
        adapter.setOnItemClickListener((position, view, vh) -> {
            startChildFragment(mShortVideoList, vh);
        });

        mPresenter.getVideoData(asStr, cpStr, ts);*/
    }

    public void initData() {
        String url = DouyinUtils.getEncryptUrl(getActivity(), 0, max_cursor);
        OkHttpClientManager.getAsyn(url, new OkHttpClientManager.StringCallback() {
            @Override
            public void onResponse(String response) {
                try {
                    DouyinVideoListData listData = DouyinVideoListData.fromJSONData(response);
                    max_cursor = listData.getMaxCursor();

                    if (listData.getVideoDataList() == null || listData.getVideoDataList().size() == 0) {
                        return;
                    }

                    mShortVideoList = listData.getVideoDataList();

                    GridLayoutManager layout = new GridLayoutManager(getActivity(), 2);
                    recycler.setLayoutManager(layout);

                    adapter = new ShortVideoAdapter(getActivity(), mShortVideoList);
                    recycler.setAdapter(adapter);
                    adapter.setOnItemClickListener((position, view, vh) -> {
                        startDouyinActivity(mShortVideoList, position, vh);
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Request request, IOException e) {
                ToastUtil.showToast(getContext(),"网络连接失败");
            }
        });
    }

    @Override
    public void showContent(DouyinVideoListData douyinVideoListData) {
        /*Log.e("ShortVideoHomeFragment", douyinVideoListData.toString());
        List<LevideoData> shortVideoList = douyinVideoListData.getVideoDataList();

        if (refresh.isRefreshing()) {
            refresh.setRefreshing(false);
            adapter.clear();
            mShortVideoList.clear();
            adapter.addAll(shortVideoList);
            recycler.setAdapter(adapter);
        }else{
            adapter.addAll(shortVideoList);
        }
        mShortVideoList.addAll(shortVideoList);*/
    }

    private void startDouyinActivity(List<LevideoData> shortVideoList, int position, RecyclerView.ViewHolder vh) {
        //启动DouyinActivity，传递数据
        JumpUtils.goToDouyinPlayer(getActivity(), shortVideoList, position);
    }

    @Override
    public void error(Throwable throwable) {

    }
}
